package com.foreveross.crawl.counter;

import java.util.HashMap;
import java.util.Map;

/**
 * 抓取引擎计数管理器
 * 
 * @author keman
 * 
 */
public class CrawlEngineCounterManager {

	/**
	 * 已注册的计数器
	 */
	private static Map<String, Counter> allCounter = new HashMap<String, Counter>();

	/**
	 * 注册一个新的计数器
	 * 
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public static synchronized Counter registerNewCounter(String name){
		if (!allCounter.containsKey(name)) {
			Counter count = new Counter(name);
			allCounter.put(name, count);
			return count;
		} else{
			return allCounter.get(name);
		}
	}

	/**
	 * 获取所有的计数器
	 * 
	 * @return
	 */
	public Map<String, Counter> getAllCounter() {
		return allCounter;
	}

	/**
	 * 重置所有的计数器
	 * 
	 * @param allCounter
	 */
	public static void resetAllCounter() {
		for(String c : allCounter.keySet()){
			resetCounter(c);
		}
	}

	/**
	 * 重置计数器 (从0开始)
	 * 
	 * @param allCounter
	 * @throws Exception
	 */
	public static void resetCounter(String name){
		if (allCounter.containsKey(name)) {
			allCounter.get(name).resetCounter();
		} 
	}

	/**
	 * 删除计数器
	 * 
	 * @param name
	 * @throws Exception 
	 */
	public static void removeCounter(String name){
		if (allCounter.containsKey(name)) {
			allCounter.remove(name);
		}
	}

}
